package com.jeeinc.oa.dao.impl;

import io.jee.medusa.orm.Hibernate4DaoSupport;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.jeeinc.oa.dao.IPortletDao;
import com.jeeinc.oa.domain.Portlet;
import com.jeeinc.oa.model.Pager;

@Repository
@SuppressWarnings("unchecked")
public class PortletDaoImpl extends Hibernate4DaoSupport<Portlet> implements IPortletDao {

	@Override
	public Pager getPortlets(Pager pager) {
		Query query = this.getSession().createQuery("from Portlet");
		query.setFirstResult((pager.getPageno() - 1) * pager.getPagesize());
		query.setMaxResults(pager.getPagesize());
		Long totalrecord = this.countAll();
		pager.setTotalrecord(totalrecord.intValue());
		pager.setLast(totalrecord.intValue() % pager.getPagesize() == 0 ? totalrecord.intValue() / pager.getPagesize() : totalrecord.intValue() / pager.getPagesize() + 1);
		pager.setTotalpage(pager.getLast());
		pager.setDatas(query.list());
		pager.setUpage(pager.getPageno() - 1 < 0 ? pager.getPageno() : pager.getPageno() - 1);
		pager.setNext(pager.getPageno() + 1 > pager.getTotalpage() ? pager.getTotalpage() : pager.getPageno() + 1 );
		return pager;
	}
}
